package cn.iocoder.yudao.module.system.controller.admin.info;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.info.vo.SystemInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.info.vo.SystemInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.info.vo.SystemInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.info.SystemInfoDO;
import cn.iocoder.yudao.module.system.service.info.SystemInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 系统信息")
@RestController
@RequestMapping("/system/info")
@Validated
public class SystemInfoController {

    @Resource
    private SystemInfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建系统信息")
    @PreAuthorize("@ss.hasPermission('system:info:create')")
    public CommonResult<Long> createInfo(@Valid @RequestBody SystemInfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统信息")
    @PreAuthorize("@ss.hasPermission('system:info:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody SystemInfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统信息")
    @PreAuthorize("@ss.hasPermission('system:info:query')")
    public CommonResult<SystemInfoRespVO> getInfo() {
        SystemInfoDO info = infoService.getInfo();
        return success(BeanUtils.toBean(info, SystemInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统信息分页")
    @PreAuthorize("@ss.hasPermission('system:info:query')")
    public CommonResult<PageResult<SystemInfoRespVO>> getInfoPage(@Valid SystemInfoPageReqVO pageReqVO) {
        PageResult<SystemInfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SystemInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid SystemInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemInfoDO> list = infoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统信息.xls", "数据", SystemInfoRespVO.class,
                        BeanUtils.toBean(list, SystemInfoRespVO.class));
    }

}